<?php
/**
 * Created by PhpStorm.
 * User: zd
 * Date: 2019/3/12
 * Time: 11:57
 */

namespace app\api\controller\v1;


use app\api\controller\BaseController;
use app\api\service\MallSetup;
use app\api\service\Token;
use app\lib\exception\CardException;
use app\lib\exception\MallException;
use app\lib\exception\SuccessMessage;

class Mall extends BaseController
{
    /**
     * 获取商场设置信息
     * @return false|string
     * @throws MallException
     * @throws \app\lib\exception\TokenException
     * @throws \think\Exception
     */
    public function getBackInfo(){
        $t_id = Token::getCurrentTokenVar('manager_id');
        $mall = new MallSetup();
        $result = $mall->getBackInfo($t_id);
        if (!$result){
            throw new MallException();
        }
        return json_encode($result);
    }

    /**
     * 更新商城信息
     * @param $data
     * @return SuccessMessage
     * @throws MallException
     */
    public function updateMall(){
        $data = input('post.');
        $t_id = Token::getCurrentUid();
        $mall = new MallSetup();
        $result =  $mall->updateMall($t_id,$data);
        if (!$result){
           throw new MallException();
        }
        return json(new SuccessMessage(),201);
    }

    /**
     * 获取商城信息
     * @return mixed
     * @throws MallException
     */
    public function getMallInfo(){
        $t_id = Token::getCurrentUid();
        $mall = new MallSetup();
        $result =  $mall->getMallInfo($t_id);
        if (empty($result)){
            throw new MallException([
                'code'=>200,
                'msg'=>'商城未设置信息',
                'error_code'=>62001

            ]);
        }
        return $result;
    }
}